package com.wnhz.work.yz.fragment.project;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.AllProject2Activity;
import com.wnhz.work.yz.activity.ProjectDetailsActivity;
import com.wnhz.work.yz.adapter.AllProject3Adapter;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.bean.AllProjectBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ---进行中
 */
public class Proceed2Fragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private AllProject2Activity activity;
    private ListView my_listView;
    private String type;
    private List<AllProjectBean> beanList = new ArrayList<>();
    private AllProject3Adapter mAdapter;

    private String user_id;

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }


    @Override
    public void onResume() {
        upload("2");
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_proceed, container, false);
        activity = (AllProject2Activity) getActivity();
        //   upload("2");
        my_listView = (ListView) view.findViewById(R.id.my_listView);
        return view;
    }

    public void upload(final String type) {
        activity.showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        if (user_id != null) {
            map.put("user_id", user_id);
        }
        XUtil.Post(Url.BUDGET_GETBUDGETLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("1222222", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    beanList.clear();
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            AllProjectBean bean = new AllProjectBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), AllProjectBean.class);
                            beanList.add(bean);
                        }
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                activity.closeDialog();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                activity.closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void initView() {
        mAdapter = new AllProject3Adapter(getActivity(), beanList, this);
        my_listView.setAdapter(mAdapter);
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        int index;
        switch (v.getId()) {
            //项目详情
            case R.id.ll_click:
                index = (int) v.getTag();
                intent = new Intent(getActivity(), ProjectDetailsActivity.class);
                intent.putExtra("budget_id", beanList.get(index).getBudget_id());
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
